<?php
/**
 * 发送消息.
 */
use Slince\SmartQQ\Client;
use Slince\SmartQQ\Message\Request\GroupMessage;
use Slince\SmartQQ\Message\Content;
use Slince\SmartQQ\Credential;
include __DIR__.'/bootstrap.php';
include 'config.php';
try {
    $db = new PDO($dsn, $username, $password); // also allows an extra parameter of configuration
} catch(PDOException $e) {
    die('Could not connect to the database:<br/>' . $e);
}
$time = time();
if (isset($_POST['member_id'])) {
    $result = $db->prepare('SELECT * FROM `sm_member` WHERE  `expire_time` > :expire_time AND `type`=1 AND `id`=:id');
    $result->bindParam(':id',$_POST['member_id']);
}else {
    $result = $db->prepare('SELECT * FROM `sm_member` WHERE  `expire_time` > :expire_time AND `type`=1');
}
$result->bindParam(':expire_time',$time);
$result->execute();
$info = $result->fetchAll(PDO::FETCH_ASSOC);
foreach ($info as $key => $value) {
//    try{
//
//        $db->beginTransaction();
    // 查询机器人有没开启这个奖励
    $result = $db->prepare('SELECT * FROM `sm_member_data_type` WHERE  `member_id` = :member_id AND `data_type`=:data_type');
    $result->bindParam(':member_id',$value['id']);
    $result->bindParam(':data_type',$_POST['data_type']);
    $result->execute();
    $hasOpen = $result->fetch(PDO::FETCH_ASSOC);
    echo $hasOpen == true ? $value['id'].'~~'.$_POST['data_type'].'~~'.'hasOpen:1'."\n" :  $value['id'].'~~'.$_POST['data_type'].'~~'.'hasOpen:0'."\n";
    if ($hasOpen) {
        $result = $db->prepare('SELECT * FROM `sm_data_member` WHERE `data_id` = :data_id AND `member_id` = :member_id');
        $result->bindParam(':data_id',$_POST['data_id']);
        $result->bindParam(':member_id',$value['id']);
        $result->execute();

        $hasSend = $result->fetch(PDO::FETCH_ASSOC);
        echo $hasSend == true ? $_POST['data_id'].'~~'.$value['id'].'~~'.'hasSend:1'."\n" :  $_POST['data_id'].'~~'.$value['id'].'~~'.'hasSend:0'."\n";
        echo $_POST['msg'];
        if (!$hasSend) {
            //创建smartQQ客户端
            $s = Credential::fromArray(json_decode($value['array'], true));
            $smartQQ = new Client($s);
            $groups = $smartQQ->getGroups();

            $group = $groups->firstByAttribute('name', $value['group']);
            //2、生成消息

            $message = new GroupMessage($group, new Content($_POST['msg']));
            $result = $smartQQ->sendMessage($message);

            // 更新data_member
            $result = $db->prepare('INSERT INTO `sm_data_member` (`data_id` , `member_id`) VALUE (:data_id , :member_id)');
            $result->bindParam(':data_id', $_POST['data_id']);
            $result->bindParam(':member_id', $value['id']);
            $result->execute();
        }

    }
//
//        $db->commit();
//    }catch (Exception $e) {
//        $db->rollBack();
//    }

//    if ($value['group'] == '测试') {
//        $s = Credential::fromArray(json_decode($value['array'], true));
//        $smartQQ = new Client($s);
//        $groups = $smartQQ->getGroups();
//
//        $group = $groups->firstByAttribute('name', $value['group']);
//        //2、生成消息
//
//        $message = new GroupMessage($group, new Content('???????'));
//        $result = $smartQQ->sendMessage($message);
//    }

}
